﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TGB.Core.Infrastructure;
using TGBTech.Core.Models.ChungTu;
using TGBTech.Core.Models.DanhMuc;
using TGBTech.Core.Models.Models.BaoCao;
using TGBTech.Core.Models.Models.BaoCao.SoNhatKyChiThuTienModel;

namespace TGBTech.Core.Models.Services
{
    public class SoNhatKyChiThuTienService
    {
        public static String SONHATKYTHUTIEN = "SoNhatKyThuTien";
        public static String SONHATKYCHITIEN = "SoNhatKyChiTien";
        public static String TAIKHOAN = "111";
        public static int MAX_LENGTH_DOIUNG = 3;
        public SoNhatKyChiThuTien createSoNhatKyChiThuTien(DateTime? ngaybatdau, DateTime? ngayketthuc, Boolean isChi, Dictionary<String, Decimal> TaiKhoanDoiUng)
        {
            //check taikhoanso 
            //check is sochi
            String strTaiKhoans;
            TaiKhoanSo taikhoanso;
            TaiKhoanSoService taikhoansoService = new TaiKhoanSoService();
            if (isChi)
            {
                taikhoanso = taikhoansoService.getTaiKhoanSoByName(SONHATKYCHITIEN);
            }
            else
            {
                taikhoanso = taikhoansoService.getTaiKhoanSoByName(SONHATKYTHUTIEN);
            }
            TAIKHOAN = taikhoanso.TaiKhoanChinh;
            strTaiKhoans = taikhoanso.TaiKhoanDoiUng;
            //init so chi thu tien
            SoNhatKyChiThuTien result = new SoNhatKyChiThuTien();
            result.ListTaiKhoanDoiUng = TaiKhoanDoiUng;
            result.IsChi = isChi;
            //check valid datetime input
            if (ngaybatdau == null || ngayketthuc == null || ngaybatdau > ngayketthuc)
            {
                return result;
            }
            result.NgayBatDau = ngaybatdau;
            result.NgayKetThuc = ngayketthuc;
            //init dbcontext
            using (TGBTechDb db = new TGBTechDb())
            {
                //select data from ngaybatdau to ngayketthuc of 111
                var query = from e in db.Data
                            where (e.NgayCT >= ngaybatdau && e.NgayCT <= ngayketthuc) && (e.TaiKhoanNoId.StartsWith(TAIKHOAN) || e.TaiKhoanCoId.StartsWith(TAIKHOAN))
                            select e;
                //check isChi to generate data
                generateData(ref result, query, strTaiKhoans);
            }
            //return result
            return result;
        }
        
        //generate data 
        private void generateData(ref SoNhatKyChiThuTien input, IQueryable<Data> query, String strTaiKhoans) { 
            //init list data
            List<Data> listData;
            //check is chi
            if (input.IsChi)
            {
                listData = (from e in query
                            where e.TaiKhoanCoId.StartsWith(TAIKHOAN)
                            select e).ToList();
            }
            else
            {
                listData = (from e in query
                            where e.TaiKhoanNoId.StartsWith(TAIKHOAN)
                            select e).ToList();
            }
            //loop datas to generate record
            SoNhatKyChiThuTienModel record;
            foreach (var data in listData)
            {
                record = new SoNhatKyChiThuTienModel();
                input.ListModel.Add(SoNhatKyChiThuTienFactory.getModel(ref record, data, input.IsChi, strTaiKhoans));
                //calculate sum of taikhoan
                generateSumOfTaiKhoan(data, ref input, listData);
            }
        }

        //calculate sum of taikhoan
        private void generateSumOfTaiKhoan(Data data, ref SoNhatKyChiThuTien input, List<Data> listData) 
        {
            //check taikhoanid exist
            var taikhoandoiung = input.IsChi ? data.TaiKhoanNoId : data.TaiKhoanCoId;
            var findResult = input.ListTaiKhoanDoiUng.Keys.Contains(taikhoandoiung) || taikhoandoiung.Length > MAX_LENGTH_DOIUNG;
            if (findResult)
            {
                return;
            }
            //calculate sum by taikhoanid
            IEnumerable<Decimal> sumQuery;
            if (input.IsChi)
            {
                sumQuery = from e in listData
                            where e.TaiKhoanNoId.StartsWith(data.TaiKhoanNoId)
                            select e.SoTien;
            }
            else
            {
                sumQuery = from e in listData
                           where e.TaiKhoanCoId.StartsWith(data.TaiKhoanCoId)
                           select e.SoTien;
            }
            //add to sonhatkychithutien
            var listToSum = sumQuery.ToList();
            if (listToSum == null)
            {
                return;
            }
            else
            { 
                var sum = listToSum.Sum();
                input.ListTaiKhoanDoiUng.Add(taikhoandoiung, sum);
            }
        }
    }
}
